<?php 

class VlogCommand extends CConsoleCommand
{

	/**
	 * Execute the action.
	 * @param array command line parameters specific for this command
	 */
	public function run($args)
	{
		$logger = Yii::getLogger();
		
		if(@$args[0]){
			// switch on visible log
			$logger->autoFlush = 1;
			$handler = array(get_class($this), 'showLogMessage');
			if(!$logger->getEventHandlers('onFlush')->contains($handler)){
				$logger->attachEventHandler('onFlush', $handler);
			}
			Yii::trace('Visible Log [On]');
		}else{
			// switch off visible log
			Yii::trace('Visible Log [Off]');
			$logger->autoFlush = 0;
		}
		
	}
	
	public static function showLogMessage(CEvent $e){
		$logger = $e->sender;
		
		$logs = $logger->getLogs();
		foreach($logs as $log){
			echo call_user_func_array(array(__CLASS__, 'formatLogMessage'), $log);
		}
	}
	

	/**
	 * Formats a log message given different fields.
	 * @param string $message message content
	 * @param integer $level message level
	 * @param string $category message category
	 * @param integer $time timestamp
	 * @return string formatted message
	 */
	protected static function formatLogMessage($message,$level,$category,$time)
	{
		$msg = "[$level] [$category] $message\n";
		if(PHP_OS == 'WINNT'){
			$msg = iconv('utf-8', 'cp866', $msg);
		}
		return $msg;
	}
}

?>